package com.restaurant.web;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.restaurant.domain.Modifiremaster;
import com.restaurant.service.ModifiremasterService;
import com.restaurant.validators.ModifireDeleteFormValidator;
import com.restaurant.validators.ModifireFormValidator;

@Component
@RequestMapping("/action/modifiremaster.do")
@SessionAttributes("modifiremaster")
public class ModifireFormController {

	private final String queryLoadAllModifires = "from Modifiremaster where isdeleted = false";
	
	@Autowired
	private ModifiremasterService objModifiremasterService;
	
	@Autowired
	private ModifireFormValidator modifireFormValidator;
	
	@Autowired
	private ModifireDeleteFormValidator modifireDeleteFormValidator;
	
	@RequestMapping(method=RequestMethod.GET)
	public String modifireGETRequest(ModelMap map){
		Modifiremaster modifiremaster = new Modifiremaster();
		
		ArrayList<Modifiremaster> listModifiremasters = null;
		
		try {
			listModifiremasters = (ArrayList<Modifiremaster>) objModifiremasterService.loadByHql(queryLoadAllModifires);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		map.addAttribute("modifiremaster", modifiremaster);
		map.addAttribute("listModifiremasters", listModifiremasters);
		
		return "Modifire";
	}
	
	@RequestMapping(method = RequestMethod.POST)
	public String modifirePOSTRequest(ModelMap map, @ModelAttribute Modifiremaster modifiremaster, BindingResult result, HttpServletRequest request, HttpServletResponse response){
		String action = request.getParameter("action");
		
		if(action.equals("addnew")){
			modifireFormValidator.validate(modifiremaster, result);
			ArrayList<Modifiremaster> listModifiremasters = null;
			if(result.hasErrors()){
				try {
					listModifiremasters = (ArrayList<Modifiremaster>) objModifiremasterService.loadByHql(queryLoadAllModifires);
				} catch (Exception e) {
					e.printStackTrace();
				}
				map.addAttribute("listModifiremasters", listModifiremasters);
				return "Modifire";
			}
			map = addNew(map, modifiremaster, request, response);
		}else if(action.equals("edit")){
			map = edit(map, modifiremaster, request, response);
		}else if(action.equals("del")){
			try {
				modifiremaster = objModifiremasterService.loadByPrimaryKey(Long.parseLong(request.getParameter("id")));
			} catch (Exception e1) {
				e1.printStackTrace();
			}
			modifireDeleteFormValidator.validate(modifiremaster, result);
			ArrayList<Modifiremaster> listModifiremasters = null;
			if(result.hasErrors()){
				try {
					listModifiremasters = (ArrayList<Modifiremaster>) objModifiremasterService.loadByHql(queryLoadAllModifires);
				} catch (Exception e) {
					e.printStackTrace();
				}
				map.addAttribute("listModifiremasters", listModifiremasters);
				return "Modifire";
			}
			map = delete(map, modifiremaster, request, response);
		}else{
			return "redirect:modifiremaster.do";
		}
		
		ArrayList<Modifiremaster> listModifiremasters = null;
		
		try {
			listModifiremasters = (ArrayList<Modifiremaster>) objModifiremasterService.loadByHql(queryLoadAllModifires);
		} catch (Exception e) {
			e.printStackTrace();
		}
				
		map.addAttribute("listModifiremasters", listModifiremasters);
		
		return "Modifire";
	}
	
	private ModelMap addNew(ModelMap map, Modifiremaster modifiremaster, HttpServletRequest request, HttpServletResponse response){
		modifiremaster.setIsdeleted(false);
		try {
			objModifiremasterService.addOrModify(modifiremaster);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		map.addAttribute("modifiremaster", new Modifiremaster());
		return map;
	}
	
	private ModelMap edit(ModelMap map, Modifiremaster modifiremaster, HttpServletRequest request, HttpServletResponse response){
		Long modifiremasterID = Long.parseLong(request.getParameter("id"));
		try {
			modifiremaster = objModifiremasterService.loadByPrimaryKey(modifiremasterID);
		} catch (Exception e) {
			e.printStackTrace();
		}
		map.addAttribute("modifiremaster", modifiremaster);
		return map;
	}
	
	private ModelMap delete(ModelMap map, Modifiremaster modifiremaster, HttpServletRequest request, HttpServletResponse response){
		Long modifiremasterID = Long.parseLong(request.getParameter("id"));
		try {
			modifiremaster = objModifiremasterService.loadByPrimaryKey(modifiremasterID);
			modifiremaster.setIsdeleted(true);
			modifiremaster = objModifiremasterService.addOrModify(modifiremaster);
		} catch (Exception e) {
			e.printStackTrace();
		}
		map.addAttribute("modifiremaster", new Modifiremaster());
		return map;
	}
}
